﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using YJKGridTopologyCal.Geometries.Utilities;

namespace YJKGridTopologyCal.Operation.Overlay.Validate
{
	public class OffsetPointGenerator
	{
		public OffsetPointGenerator(IGeometry g)
		{
			this._g = g;
		}

		public void SetSidesToGenerate(bool doLeft, bool doRight)
		{
			this._doLeft = doLeft;
			this._doRight = doRight;
		}

		public List<Coordinate> GetPoints(double offsetDistance)
		{
			List<Coordinate> list = new List<Coordinate>();
			foreach (IGeometry geometry in LinearComponentExtracter.GetLines(this._g))
			{
				ILineString line = (ILineString)geometry;
				this.ExtractPoints(line, offsetDistance, list);
			}
			return list;
		}

		private void ExtractPoints(ILineString line, double offsetDistance, IList<Coordinate> offsetPts)
		{
			ICoordinateSequence coordinateSequence = line.CoordinateSequence;
			int num = coordinateSequence.Count - 1;
			for (int i = 0; i < num; i++)
			{
				this.ComputeOffsetPoints(coordinateSequence.GetCoordinate(i), coordinateSequence.GetCoordinate(i + 1), offsetDistance, offsetPts);
			}
		}

		private void ComputeOffsetPoints(Coordinate p0, Coordinate p1, double offsetDistance, IList<Coordinate> offsetPts)
		{
			double num = p1.X - p0.Y;
			double num2 = p1.Y - p0.Y;
			double num3 = Math.Sqrt(num * num + num2 * num2);
			double num4 = offsetDistance * num / num3;
			double num5 = offsetDistance * num2 / num3;
			double num6 = (p1.X + p0.X) / 2.0;
			double num7 = (p1.Y + p0.Y) / 2.0;
			if (this._doLeft)
			{
				Coordinate item = new Coordinate(num6 - num5, num7 + num4);
				offsetPts.Add(item);
			}
			if (this._doRight)
			{
				Coordinate item2 = new Coordinate(num6 + num5, num7 - num4);
				offsetPts.Add(item2);
			}
		}

		private bool _doLeft = true;

		private bool _doRight = true;

		private readonly IGeometry _g;
	}
}
